﻿using Platformer_Example.Content.Images.Player;
using Platformer_Example.Controller.Character.Attack;
using Platformer_Example.Controller.Input;
using System;
using Transformable_Engine_v2.Engine.Animation.FrameAnimation;
using Transformable_Engine_v2.Engine.Global;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Platformer_Example.Controller.Character.Player
{
    public partial class Player
    {
        private FrameAnimation _attackAnimation;
        private Timer _attackTimer;

        protected override void CreateAttackStateObjects()
        {
            _attackTimer = new Timer(0, false, EndAttack);
            AddTimers(_attackTimer);
        }

        protected override void InitializeAttackState()
        {
            ResetMovement();

            var anim = new PlayerAnimations.Normal.Attack(2);
            Character.AddAnimations(_attackAnimation = new FrameAnimation(anim));

            Character.OriginAbsolute = CurrentDirection == Direction.Right ? new TE_Vector2(8, 32) : new TE_Vector2(24, 32);
            _attackAnimation.OnChangeFrame += OnChangeAttackFrame;

            _attackTimer.Reset(anim.Duration);
        }

        private readonly AttackDescription[] _attackDescriptions = new[]
                                                                       {
                                                                           null,
                                                                           new AttackDescription(AttackDescription.AttackType.Normal, AttackDescription.AttackTargets.NotInTeam, new TE_Rectangle(1, -25, 16, 16), new TE_Vector2(), 300),
                                                                           new AttackDescription(AttackDescription.AttackType.Normal, AttackDescription.AttackTargets.NotInTeam, new TE_Rectangle(1, -24, 19, 16), new TE_Vector2(), 300),
                                                                           new AttackDescription(AttackDescription.AttackType.Normal, AttackDescription.AttackTargets.NotInTeam, new TE_Rectangle(1, -21, 23, 17), new TE_Vector2(), 300),
                                                                           new AttackDescription(AttackDescription.AttackType.Normal, AttackDescription.AttackTargets.NotInTeam, new TE_Rectangle(1, -13, 20, 15), new TE_Vector2(), 300),
                                                                           null,
                                                                       };

        private void OnChangeAttackFrame(int frameId)
        {
            if (_attackDescriptions[frameId] != null)
                CurrentMap.Attack(_attackDescriptions[frameId], this);
        }

        protected override void UpdateAttackState(TimeSpan gameTime)
        {
        }

        void EndAttack()
        {
            Behavior.EndAttack();
        }

        protected override void EndAttackState()
        {
            _attackAnimation.OnChangeFrame -= OnChangeAttackFrame;
        }

        protected override void OnKeyPressAttack(InputState.Key key)
        {
        }

        protected override void OnKeyReleaseAttack(InputState.Key key)
        {
        }

        public override TE_Rectangle CollisionLadder
        {
            get { return _collisionLadder; }
        }

        protected override TE_Rectangle CollisionWarp
        {
            get { return _collisionWarp; }
        }
    }
}